package com.zx.controller;

import com.zx.dao.UserMapper;
import com.zx.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    @Autowired
    private UserMapper userMapper;





    @RequestMapping("/login")
    public String index(){
        return "index.html";
    }

    @RequestMapping("/red")
    public String red(){
        return "error.html";
    }

    /**
     * 登录 与查询
     * @param user
     * @param model
     * @return
     */
    @RequestMapping("loginuser")
    public String loginuser(User user ,Model model){
        List<User> loginuserto=userMapper.LoginUser(user);
        if(loginuserto.size()>0){
            //查询用户
            List<User> listuser=userMapper.selectUser();
            model.addAttribute("listuser",listuser);
            return "main.html";
        }else {
            model.addAttribute("error","登录失败");
            return "error.html";
        }

    }




    /**
     * 注册用户
     * @param req
     * @param user
     * @param regpwd
     * @param model
     * @return
     */
    @RequestMapping("/reglogin")
    public String reglogin(HttpServletRequest req,User user , String regpwd, Model model){
        if(user.getPwd()!=null&&user.getUsername()!=null&&regpwd!=null){
            if(user.getPwd().equals(regpwd)){
                List<User> fituser= this.userMapper.Fituser(user);
                if(fituser.size()>0){
                    req.setAttribute("error","以注册");
                    return "error.html";
                }
                try{
                    this.userMapper.adduser(user);
                    //model.addAttribute("error","成功");
                    req.setAttribute("error","成功");
                    return "error.html";
                }catch (Exception e){
                    e.printStackTrace();
                    //model.addAttribute("error","失败");
                    req.setAttribute("error","失败");
                    return "error.html";
                }
            }else {
               // model.addAttribute("error","两次密码一致");
                req.setAttribute("error","两次密码一致");
                return "error.html";
            }

        }else {
            //model.addAttribute("error","不能为空");
            req.setAttribute("error","不能为空");
            return "error.html";
        }
    }


    /**
     * 删除
     * @param user
     * @return
     */
    @RequestMapping("deleteuser")
    @ResponseBody
    public String DeleteUser(User user){
        try {
            String obj="1";
            this.userMapper.Delete(user);
            return obj;
        }catch (Exception e){
            e.printStackTrace();
            String obj="0";
            return obj;
        }
    }


    /**
     * id查询
     * @param user
     * @return
     */
    @RequestMapping("Iduser")
    @ResponseBody
    public Map<String,Object> Iduser(User user){
          Map<String,Object> iduser=  this.userMapper.IdselectUser(user);
          return iduser;

    }
    @RequestMapping("upuser")
    @ResponseBody
    public String upuser(User user){
        try {
            String obj="1";
            this.userMapper.update(user);
            return obj;
        }catch (Exception e){
            e.printStackTrace();
            String obj="0";
            return obj;
        }
    }


}
